// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

//
var selected_recents = []
var selected_artifacts = []

function reset_selections()
{
	selected_recents = []
	selected_artifacts = []
}

//Handle clicks on artifacts
function click(domid)
{
    list_item = document.getElementById(domid)
	color =  list_item.style.color
	if (color == 'grey')
	{
		list_item.style.color = 'black'
	}	  
	else
	{
	    list_item.style.color = 'grey'	
		type = domid.split('_').first()
		id = domid.split('_').last()
		if(type == 'recent')
		{
			if(selected_recents.length == 0)
			{
				selected_recents = new Array(id)
			}
			else
			{
				selected_recents = selected_recents.concat(new Array(id))
			}			
		}
		else // assume list at present
		{
			if(selected_artifacts.length == 0)
			{
				selected_artifacts = new Array(id)
			}
			else
			{
				selected_artifacts = selected_artifacts.concat(new Array(id))
			}			
		}

	}
}

//add selected items to context
function add_selected_to_context()
{
	if(selected_artifacts.length > 0)
	{
	    new Ajax.Updater('context', 
	                 '/artifacts/add_context', 
					 {asynchronous:true, 
					       evalScripts:true, 
						   onComplete:function(request){eval(request.responseText)}, 
						   parameters:'list_items=' + encodeURIComponent(selected_artifacts.join("/"))})
        selected_artifacts = []
// derived from the following
//						   
//					   <%=remote_function(:url => {:controller=>'artifacts', :action=>'add_context'},
//											:with => "'list_item=' + encodeURIComponent(element.id)" , 
//											:complete => evaluate_remote_response, 
//											:update => 'context')%>
	}
}

//add selected items to location
function add_selected_to_location()
{
	if(selected_recents.length > 0)
	{
	    new Ajax.Updater('artifacts', 
	                 '/artifacts/add_relations', 
					 {asynchronous:true, 
					       evalScripts:true, 
						   onComplete:function(request){eval(request.responseText)}, 
						   parameters:'list_items=' + encodeURIComponent(selected_recents.join("/"))})
        selected_recents = []
// derived from the following
//						   
//					   <%=remote_function(:url => {:controller=>'artifacts', :action=>'add_relations'},
//											:with => "'list_item=' + encodeURIComponent(element.id.split('_').last())" , 
//											:complete => evaluate_remote_response, 
//											:update => 'artifacts')%>
	}
}

